﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using He.Web.UI;
using System.Text;
using He.Common;

namespace He.Web.manage.nav
{
    public partial class list : He.Web.UI.Manage
    {
        public string lang;
        public string keywords = string.Empty;
        public string property = string.Empty;

        protected void Page_Load(object sender, EventArgs e)
        {
            lang = GetString("lang");
            keywords = GetString("keywords");
            property = GetString("property");
            if (!IsPostBack)
            {
                ddlLang.DataSource = LangHelper.GetLangDict();
                ddlLang.DataTextField = "Value";
                ddlLang.DataValueField = "Key";
                ddlLang.DataBind();

                RptBind("id>0" + Comb_SqlTxt(lang, keywords, property));
            }
        }

        #region 数据绑定
        private void RptBind(string _strWhere)
        {
            this.ddlLang.SelectedValue = this.lang.ToString();
            this.ddlProperty.SelectedValue = this.property;
            this.txtKeywords.Text = this.keywords;
            this.rptList.DataSource = new He.BLL.nav().GetList(_strWhere);
            this.rptList.DataBind();
        }
        #endregion

        #region 组合SQL查询语句
        protected string Comb_SqlTxt(string _lang, string _keywords, string _property)
        {
            StringBuilder strTemp = new StringBuilder();
            _keywords = _keywords.Replace("'", "");
            if (!string.IsNullOrEmpty(_lang))
            {
                strTemp.AppendFormat(" and lang='{0}'", _lang);
            }
            if (!string.IsNullOrEmpty(_keywords))
            {
                strTemp.Append(" and title like '%" + _keywords + "%'");
            }
            if (!string.IsNullOrEmpty(_property))
            {
                switch (_property)
                {
                    case "is_lock":
                        strTemp.Append(" and is_lock=true");
                        break;
                    case "un_lock":
                        strTemp.Append(" and is_lock=0");
                        break;
                }
            }
            strTemp.Append(" order by sort_id asc");
            return strTemp.ToString();
        }
        #endregion

        #region 组合URL语句
        protected string Comb_UrlTxt(string _lang, string _keywords, string _property)
        {
            StringBuilder strTemp = new StringBuilder();
            _keywords = _keywords.Replace("'", "");
            if (!string.IsNullOrEmpty(_lang))
            {
                strTemp.Append("lang=" + _lang + "&");
            }
            if (!string.IsNullOrEmpty(_keywords))
            {
                strTemp.Append("keywords=" + HttpContext.Current.Server.UrlEncode(_keywords) + "&");
            }
            if (!string.IsNullOrEmpty(_property))
            {
                strTemp.Append("property=" + _property + "&");
            }
            return strTemp.ToString().Trim('&');
        }
        #endregion

        //语言
        protected void ddlLang_SelectedIndexChanged(object sender, EventArgs e)
        {
            Response.Redirect("list.aspx?" + Comb_UrlTxt(this.ddlLang.SelectedValue, this.keywords, property));
        }
        //属性
        protected void ddlProperty_SelectedIndexChanged(object sender, EventArgs e)
        {
            Response.Redirect("list.aspx?" + Comb_UrlTxt(this.lang, this.keywords, ddlProperty.SelectedValue));
        }

        //搜索
        protected void btnSelect_Click(object sender, EventArgs e)
        {
            Response.Redirect("list.aspx?" + Comb_UrlTxt(this.lang, this.txtKeywords.Text.Trim(), property));
        }

        //删除操作
        protected void lbtnDel_Click(object sender, EventArgs e)
        {
            He.BLL.nav bll = new He.BLL.nav();
            for (int i = 0; i < rptList.Items.Count; i++)
            {
                CheckBox cb = (CheckBox)rptList.Items[i].FindControl("Check");
                int id = int.Parse(((Label)rptList.Items[i].FindControl("lb_id")).Text);
                if (cb.Checked)
                {
                    Model.nav model = bll.GetModel(id);
                    if (model.is_protected == 1)
                    {
                        JscriptMsg(350, 230, "操作失败", "<b>错误提示！</b>您要删除的数据受系统保护。", "back", "Error");
                        continue;
                    }
                    FileHelper.DeleteFile(model.img_url);
                    bll.Delete(id);
                    SaveLogs("[系统频道]删除导航栏目：" + model.title);
                }
            }
            JscriptPrint("批量删除成功啦！", "list.aspx?" + Comb_UrlTxt(this.lang, this.txtKeywords.Text.Trim(), property), "Success");
        }

        //保存排序
        protected void lbtnSave_Click(object sender, EventArgs e)
        {
            He.BLL.nav bll = new He.BLL.nav();
            for (int i = 0; i < rptList.Items.Count; i++)
            {
                CheckBox cb = (CheckBox)rptList.Items[i].FindControl("Check");
                int id = int.Parse(((Label)rptList.Items[i].FindControl("lb_id")).Text);
                int sortId;
                if (!int.TryParse(((TextBox)rptList.Items[i].FindControl("txtSortId")).Text.Trim(), out sortId))
                {
                    sortId = 99;
                }
                bll.UpdateField(id, "sort_id=" + sortId);
            }
            JscriptPrint("排序保存成功啦！", "list.aspx?" + Comb_UrlTxt(this.lang, this.txtKeywords.Text.Trim(), property), "Success");
        }
    }
}